package net.com.hydroPK.model.skills;

import net.com.hydroPK.Engine;
import net.com.hydroPK.model.Player;

/**
 * File created by: Joshua
 * Created on 24/2/2010 at 0:8
 */
public class Woodcutting {

    Player p;
    public Woodcutting(Player p) {
        this.p = p;
    }

    public int type; // 0 = Normal logs
    public int[] logs = {
            1511, 1517, 1515, 1513
    };
    public int[] exp = {
            700, 1653, 2200, 4250
    };
    public int[] level = {
            1, 46, 65, 76
    };
    public int[] time = {
            1000, 1200, 1220, 1300
    };
    public boolean woodcutting = false;
    public long lastLog = 0;

    public void startWoodcutting(int objectId) {
        resetWoodcutting();
        type = getTypeForTree(objectId);
        if (!hasAxe()) {
            p.message("You need an axe you are capable of using to cut down this tree.");
            return;
        }
        if (!hasLevel(type)) {
            p.message("You need the required level of " + level[type] + " to cut down this tree.");
            return;
        }
        if (Engine.playerItems.freeSlotCount(p) == 0) {
            p.message("You have ran out of inventory space.");
            return;
        }
        p.message("You start woodcutting...");
        woodcutting = true;
        if (System.currentTimeMillis() - lastLog >= time[type]) {
            woodcutLog();
        } else {
            p.requestAnim(getEmote(), 0);
        }
    }
    public void woodcutLog() {
        if (!hasAxe()) {
            p.message("You need an axe you are capable of using to cut down this tree.");
            resetWoodcutting();
            p.requestAnim(p.standEmote, 0);
            return;
        }
        if (!hasLevel(type)) {
            p.message("You need the required level of " + level[type] + " to cut down this tree.");
            resetWoodcutting();
            p.requestAnim(p.standEmote, 0);
            return;
        }
        if (Engine.playerItems.freeSlotCount(p) <= 0) {
            p.message("You have ran out of inventory space.");
            resetWoodcutting();
            p.requestAnim(p.standEmote, 0);
            return;
        }
        Engine.playerItems.addItem(p, logs[type], 1);
        p.message("You chop a log");
        p.appendExperience(exp[type], 8);
        lastLog = System.currentTimeMillis();
        p.requestAnim(getEmote(), 0);
    }
    public void resetWoodcutting() {
        woodcutting = false;
        type = -1;
    }
    public void process() {
        if (woodcutting) {
            if (type == -1) {
                resetWoodcutting();
                p.requestAnim(p.standEmote, 0);
                return;
            }
            if (System.currentTimeMillis() - lastLog >= time[type]) {
                woodcutLog();
            }
        }
    }
    public boolean hasAxe() {
        if ((Engine.playerItems.hasPlayerItem(p, 6739) || p.equipment[p.weaponNum] == 6739)
                && p.getLevelForXP(8) >= 60) {
            return true;
        } else if (Engine.playerItems.hasPlayerItem(p, 1359) || p.equipment[p.weaponNum] == 1359) {
            return true;
        }
        return false;
    }
    public boolean hasLevel(int type) {
        return p.getLevelForXP(8) >= level[type];
    }
    public int getTypeForTree(int tree) {
        switch (tree) {
            case 1276: // Regular
                return 0;
            case 1307: // Maple
                return 1;
            case 1309: // Yew
                return 2;
            case 1306: // Magic
                return 3;
        }
        return -1;
    }
    public int getEmote() {
        if ((Engine.playerItems.hasPlayerItem(p, 6739) || p.equipment[p.weaponNum] == 6739)
                && p.getLevelForXP(8) >= 60) {
            return 2846;
        } else if (Engine.playerItems.hasPlayerItem(p, 1359) || p.equipment[p.weaponNum] == 1359) {
            return 867;
        }
        return 867;
    }
}
